From bf5a3a5c8c5a0e08f002ff73d4a48a47d613ed09 Mon Sep 17 00:00:00 2001 From: robertlipe Date: Wed, 7 Aug 2013 19:06:32 +0000 Subject: [PATCH] From Conrad Meyer: - Nuke leftover HAVE_EXPAT garbage in google.cc - Use file prefix to attempt to discover HTML encoding - Slurp entire HTML input file as discovered encoding, then pass to QXmlStreamReader as a QString xmlgeneric: - Adds a new function, void xml_readunicode(const QString& str) (does what you'd expect -- feeds QString input into the xml parser and runs it) git-svn-id: http://gpsbabel.googlecode.com/svn/trunk@4519 f51c46e8-681c-474f-0cfe-069cfd0219fb --- gpsbabel/defs.h | 2 +- gpsbabel/google.cc | 62 ++++++++++++++++++++++++++++++++---------- gpsbabel/xmlgeneric.cc | 14 ++++++++++ gpsbabel/xmlgeneric.h | 1 + 4 files changed, 63 insertions(+), 16 deletions(-) diff --git a/gpsbabel/defs.h b/gpsbabel/defs.h index e0eb3cd06..6380c6941 100644 --- a/gpsbabel/defs.h +++ b/gpsbabel/defs.h @@ -36,7 +36,7 @@ #include "inifile.h" #include "session.h" -#include +#include #include # include "src/core/datetime.h" diff --git a/gpsbabel/google.cc b/gpsbabel/google.cc index ed1387f36..99ec30004 100644 --- a/gpsbabel/google.cc +++ b/gpsbabel/google.cc @@ -17,6 +17,7 @@ */ +#include #include #include "defs.h" @@ -28,25 +29,13 @@ static char* script = NULL; static route_head** routehead; static int* routecount; static short_handle desc_handle; +static const char* rd_fname; static int serial = 0; #define MYNAME "google" #define MY_CBUF 4096 -#if ! HAVE_LIBEXPAT -static void -google_rd_init(const char* fname) -{ - fatal(MYNAME ": This build excluded Google Maps support because expat was not installed.\n"); -} - -static void -google_read(void) -{ -} -#else - static xg_callback goog_points, goog_levels, goog_poly_e, goog_script; static xg_callback goog_segment_s, goog_segment, goog_td_s, goog_td_b; static xg_callback goog_td_e; @@ -259,19 +248,63 @@ void goog_poly_e(const char* args, const QXmlStreamAttributes *unused) static void google_rd_init(const char* fname) { + rd_fname = fname; + desc_handle = mkshort_new_handle(); setshort_length(desc_handle, 12); xml_init(fname, google_map, NULL); } +static void +goog_read_file(void) +{ + QFile src(QString::fromUtf8(rd_fname)); + + src.open(QIODevice::ReadOnly); + + QTextStream tstr(&src); + tstr.setCodec("ISO-8859-1"); + + QString preamble = tstr.read(256); + QString needle("http-equiv=\"content-type\" content=\"text/html; charset="); + + if (!preamble.contains(needle)) { + // let QXmlStreamReader do its best if we can't figure it out... + xml_read(); + return; + } + + int idx = preamble.indexOf(needle); + QString charset = preamble.mid(idx + needle.length()); + + int endq = charset.indexOf('"'); + if (endq != -1) { + charset = charset.left(endq); + } + + QString wholefile; + if (charset == "ISO-8859-1") { + wholefile = preamble + tstr.readAll(); + } else { + tstr.reset(); + tstr.seek(0); + tstr.setCodec(CSTR(charset)); + wholefile = tstr.readAll(); + } + + xml_readunicode(wholefile); +} + static void google_read(void) { routehead = (route_head**)xmalloc(sizeof(route_head*)); routecount = (int*)xmalloc(sizeof(int)); goog_segroute = 0; - xml_read(); + + goog_read_file(); + xfree(routehead); xfree(routecount); routehead = NULL; @@ -512,7 +545,6 @@ google_read(void) encoded_levels = NULL; } } -#endif static void google_rd_deinit(void) diff --git a/gpsbabel/xmlgeneric.cc b/gpsbabel/xmlgeneric.cc index 88717f1a4..abd712d16 100644 --- a/gpsbabel/xmlgeneric.cc +++ b/gpsbabel/xmlgeneric.cc @@ -272,11 +272,15 @@ void xml_ignore_tags(const char** taglist) } } +// Chucks some bytes into the global QByteArray buffer and waits for +// xml_readstring() to parse. void xml_readprefixstring(const char* str) { reader_data.append(str); } +// Parses a bytestream as if it were a file. Looks for an